package h0720;

import java.util.Scanner;
import java.util.StringJoiner;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 螺旋数字矩阵
 * @date 2024/7/20 12:47
 */
public class SpiralNumericalMatrixII {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        //优化点
        int quotient = (n + m - 1) / m;  //用这个算式直接取整。

        //确定行列。
        int row = m;
        int col = quotient;
        String[][] matrix = getMatrix(row, col, n);

        for (String[] re : matrix) {
            StringJoiner joiner = new StringJoiner(" ");
            for (String s : re) {
                joiner.add(s);
            }
            System.out.println(joiner);
        }
    }

    private static String[][] getMatrix(int row, int col, int n) {
        int left = 0;
        int right = col - 1;
        int up = 0;
        int down = row - 1;

        int[][] ans = new int[row][col];
        String[][] res = new String[row][col];
        int temp = 1;
        while (left <= right && up <= down) {

            for (int i = left; i <= right; i++) {
                ans[up][i] = temp;
                res[up][i] = temp > n ? "*" : Integer.toString(temp);
                temp++;
            }
            up++; //下移一层
            for (int i = up; i <= down; i++) {
                ans[i][right] = temp;
                res[i][right] = temp > n ? "*" : Integer.toString(temp);
                temp++;
            }
            right--; //right左移
            if (up<=down){
                for (int i = right; i >= left; i--) {
                    ans[down][i] = temp;
                    res[down][i] = temp > n ? "*" : Integer.toString(temp);
                    temp++;
                }
                down--;
            }
            if (left<=right){
                for (int i = down; i >= up; i--) {
                    ans[i][left] = temp;
                    res[i][left] = temp > n ? "*" : Integer.toString(temp);
                    temp++;
                }
                left++; //left左移
            }
        }
        return res;
    }
}
